﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Ensyc;
using Ensyc.Reader;

namespace ControlDeAcceso
{
    public partial class AsiganTAG : Form
    {
        private Ensyc.Reader.EReader mReader;
        private delegate void dlSetResponse(EReader.Response EResponse);

        public AsiganTAG()
        {
            InitializeComponent();
        }

        private void AsiganTAG_Load(object sender, EventArgs e)
        {
            ConnectReader();

            Libreria.SIG.ServiciosRFID.WebServiceRFID servicio = new Libreria.SIG.ServiciosRFID.WebServiceRFID();
            dataGridViewPersonas.DataSource = servicio.personas();
            dataGridViewPersonas.Columns[0].Visible = false;
            dataGridViewPersonas.Columns[1].Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mReader.ReadTag();
        }

        private void ConnectReader()
        {

            int[] iReaders = Utility.GetConnectedReaders();
            if (iReaders.Length > 0)
            {

                //We found one or more connected readers
                if (iReaders.Length > 1)
                {
                    MessageBox.Show("We found " + iReaders.Length.ToString() + " readers.");
                }
                else
                {
                    //iReaders[0];
                    mReader = new EReader();
                    mReader.Open(iReaders[0]);
                    mReader.ReaderResponse += new dlReaderResponse(mReader_ReaderResponse);
                    mReader.GetFirmwareVersion();
                    this.Text += " - COM" + mReader.PortNumber.ToString();
                    //MessageBox.Show("We are connected on COM" + iReaders[0].ToString());

                }
            }
            else
            {
                MessageBox.Show("No readers found. Please open this application again after connecting reader.");
                this.Close();
            }

        }

        void mReader_ReaderResponse(EReader.Response EResponse)
        {
            //throw new NotImplementedException();
            //Handle your response here.
            this.Invoke(new dlSetResponse(ProcessResponse), new object[] { EResponse });
        }

        void ProcessResponse(EReader.Response EResponse)
        {
            //Now you can do all form control related operations.
            if (EResponse.ResponseCode == EReader.CommandCode.MSGOK)
            {
                //This means command was executed successfully. 
                switch (EResponse.CommandEcho)
                {
                    case EReader.Command.READ_TAG:
                        if (EResponse.CommandSpecificResponse != null)
                        {

                            EReader.ReadTagResponse readresponse = (EReader.ReadTagResponse)EResponse.CommandSpecificResponse;
                            if (readresponse.Decode == EReader.ReadTagResponse.TagDecode.GOOD_TAG)
                            {
                                byte[] bTagData = readresponse.TagData;
                                string sTagData = Utility.ToHex(bTagData);
                                sTagData = Utility.ToHex(bTagData, true);
                                textBoxTAG.Text = sTagData;
                                sTagData = Utility.ByteToString(bTagData);
                                textBoxTAG.Text += "\r\n" + sTagData;
                            }
                            else
                            {
                                textBoxTAG.Text = "Carnet no valido " + readresponse.Decode.ToString();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Read Tag has null response");
                        }
                        break;
                    case EReader.Command.WRITE_TAG:
                        //MessageBox.Show("Tag was written. Read it.");
                        mReader.ReadTag();
                        break;
                }
            }
            else
            {
                //Handle your error gracefully.
                if (EResponse.CommandEcho == EReader.Command.WRITE_TAG)
                {
                    MessageBox.Show("Unable to write tag: " + EResponse.ResponseCode.ToString());
                }
            }
        }

    }
}
